package com.qf.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//拦截器
@Component
public class MyInterceptor implements HandlerInterceptor {
    //jdk1.8中接口的新特性  被default修饰

    @Override
    //权限校验，可以拦截当前请求，不执行controller
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了前置拦截器");
        return true;  //true,代表方行 false 代表拦截
    }

    @Override
    //当controller正常返回信息时，会执行当前方法，修改modelandview                            //异常类        //视图解析器也就是数据和路径
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("执行了中间拦截器");
    }

    @Override
    //无论controller执行失败与否，都会执行当前方法，获取异常信息，记录日志
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("执行了后置拦截器");
    }
}
